package com.zbcz.erp.adapter

import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.zbcz.erp.R
import com.zbcz.erp.activity.MyApplyHistoryActivity
import com.zbcz.erp.adapter.MyApplyToolsAdapter.Companion.extractAfterSecondLastSlash
import com.zbcz.erp.bean.DoneJobBean
import com.zbcz.erp.databinding.ItemDoingBusinessBinding
import com.zbcz.erp.fragment.CommonListFragment.Companion.DONE_JOB_BEAN
import com.zbcz.erp.fragment.CommonListFragment.Companion.RE_APPLY
import com.zbcz.erp.fragment.CommonListFragment.Companion.TYPE
import com.zbcz.erp.util.onSingleClick

class MyApplyAdapter :
    ListAdapter<DoneJobBean, MyApplyAdapter.ItemDoingBusinessViewHolder>(DiffCallback()) {

    var onItemClickListener: ((Int, DoneJobBean) -> Unit)? = null

    var onCancelClickListener: ((Int, DoneJobBean) -> Unit)? = null

    var mHistoryClickListener: ((Int, DoneJobBean) -> Unit)? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemDoingBusinessViewHolder {
        val binding =
            ItemDoingBusinessBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ItemDoingBusinessViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ItemDoingBusinessViewHolder, position: Int) {
        val item = getItem(position)
        holder.bind(item)
    }

    inner class ItemDoingBusinessViewHolder(private val binding: ItemDoingBusinessBinding) :
        RecyclerView.ViewHolder(binding.root) {

        init {
            binding.root.onSingleClick {
                onItemClickListener?.invoke(adapterPosition, currentList[adapterPosition])
            }
            binding.tvReApply.onSingleClick {
                val routeUrl = extractAfterSecondLastSlash(currentList[adapterPosition].routeName)
                ARouter.getInstance().build(routeUrl).withInt(TYPE, RE_APPLY)
                    .withParcelable(DONE_JOB_BEAN, currentList[adapterPosition]).navigation()
            }

            binding.tvCancelApply.onSingleClick {
                onCancelClickListener?.invoke(adapterPosition, currentList[adapterPosition])
            }
            binding.tvApplyHistory.onSingleClick {
                binding.root.context.startActivity(
                    Intent(
                        binding.root.context, MyApplyHistoryActivity::class.java
                    ).apply {
                        putExtra("id", currentList[adapterPosition].procInstId)
                    })
            }
        }

        fun bind(item: DoneJobBean) {
            binding.tvBusiness.text = item.title
            binding.tvApplicantValue.text = item.applyer
            binding.tvCurrentLinkValue.text = item.currTaskName ?: ""
            binding.tvStatusValue.text = when (item.status) {
                0 -> "已激活"
                1 -> "处理中"
                2 -> "已结束"
                3 -> "撤回"
                else -> ""
            }
            binding.tvStatusValue.setTextColor(
                ContextCompat.getColor(
                    binding.root.context,
                    when (item.result) {
                        1 -> R.color.yellow_doing
                        2 -> R.color.blue_text_color
                        else -> R.color.black_text
                    }
                )
            )

            binding.tvResultValue.text = when (item.result) {
                0 -> "待提交"
                1 -> "处理中"
                2 -> "通过"
                3 -> "驳回"
                else -> ""
            }
            binding.tvResultValue.setTextColor(
                ContextCompat.getColor(
                    binding.root.context,
                    when (item.result) {
                        1 -> R.color.yellow_doing
                        2 -> R.color.green_pass
                        3 -> R.color.red_reject
                        else -> R.color.black_text
                    }
                )
            )
            binding.tvReApply.isEnabled = item.result == 3
            binding.tvCancelApply.isEnabled = item.result == 1
            binding.tvTime.text = "流程ID: " + item.procDefId
        }
    }

    class DiffCallback : DiffUtil.ItemCallback<DoneJobBean>() {
        override fun areItemsTheSame(
            oldItem: DoneJobBean,
            newItem: DoneJobBean
        ): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(
            oldItem: DoneJobBean,
            newItem: DoneJobBean
        ): Boolean {
            return oldItem == newItem
        }
    }
}
